﻿using SFML.Graphics;

namespace WolfCarol.SFU
{
    public class Button : Renderer
    {
        public Transformable Transform => _sprite;

        private Sprite _sprite;

        private Action? _onClick;

        public Button(Texture texture)
        {
            _sprite = new Sprite(texture);
        }

        public void AddListener(Action action)
        {
            _onClick += action;
        }

        public void RemoveListener(Action action)
        {
            _onClick -= action;
        }

        public override void OnUpdate()
        {
            bool isContained = _sprite.GetGlobalBounds().Contains(Camera.Main.ScreenToWorldPoint(Input.MousePosition));

            if (Input.GetMouseButtonDown(MouseCode.Left) && isContained)
            {
                _onClick?.Invoke();
            }
        }

        public override void Draw(RenderTarget target, RenderStates states)
        {
            target.Draw(_sprite, states);
        }
    }
}
